package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfUserDAO;
import com.ljkj.common.db.domain.ZjfGoodsAd;
import com.ljkj.common.db.domain.ZjfUser;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class ZjfUserService {
	
	private ZjfUserDAO zjfUserDAO;
	private TLogService tLogService;
	
	
	/**
	 * 获取生成用户
	 * @param params 权限名称关键字
	 * @param currentPage 当前页
	 * @param pageSize 每页条数
	 * @param sortField 排序字段
	 * @param sortOrder 排序方式
	 * @return
	 */
	public String getZjfUsersByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {ZjfUser.class};
		String[] propertyNames=null;
		Object[] values=null;
		String[] likePropertyNames =  {"userName"};
		Object[] likevalues= {params};
		String[] betweenPropertyNames= null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfUserDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfUserDAO.setPageMaxCount(pageSize);
		List<ZjfUser> zjfUsers = this.zjfUserDAO.findByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfUser zjfUser : zjfUsers){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("userId",zjfUser.getUserId());
			entry_map.put("userRoleId",zjfUser.getUserRoleId());
			entry_map.put("userLvId",zjfUser.getUserLvId());
			entry_map.put("userMobile",zjfUser.getUserMobile());
			entry_map.put("userTel",zjfUser.getUserTel());
			entry_map.put("userEmail",zjfUser.getUserEmail());
			entry_map.put("userZipCode",zjfUser.getUserZipCode());
			entry_map.put("userCountry",zjfUser.getUserCountry());
			entry_map.put("userProvince",zjfUser.getUserProvince());
			entry_map.put("userArea",zjfUser.getUserArea());
			entry_map.put("userAddress",zjfUser.getUserAddress());
			entry_map.put("userBornYear",zjfUser.getUserBornYear());
			entry_map.put("userBornMonth",zjfUser.getUserBornMonth());
			entry_map.put("userBornDay",zjfUser.getUserBornDay());
			entry_map.put("user_webchat_openID",zjfUser.getUser_webchat_openID());
			entry_map.put("userWebcahtHeadurl",zjfUser.getUserWebcahtHeadurl());
			entry_map.put("userQqNumber",zjfUser.getUserQqNumber());
			entry_map.put("userQqHeadurl",zjfUser.getUserQqHeadurl());
			entry_map.put("userLastLogin",zjfUser.getUserLastLogin());
			entry_map.put("userName",zjfUser.getUserName());
			entry_map.put("userRealName",zjfUser.getUserRealName());
			entry_map.put("userCity",zjfUser.getUserCity());
			entry_map.put("userSex",zjfUser.getUserSex());
			entry_map.put("userPoInt",zjfUser.getUserPoInt());
			entry_map.put("userGoodsValues",zjfUser.getUserGoodsValues());
			entry_map.put("userBalance",zjfUser.getUserBalance());
			entry_map.put("createTime",zjfUser.getCreateTime());
			entry_map.put("createUser", zjfUser.getCreateUser());
			entry_map.put("updateTime", zjfUser.getUpdateTime());
			entry_map.put("updateUser", zjfUser.getUpdateUser());
			entry_map.put("disabledState", zjfUser.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	
	
	/**
	 * 新增或更新广告
	 * @param zdgCoupon
	 * @return
	 */
	public String addOrEditZjfUserInfo(ZjfUser zjfUser,Tuser tuser){
		
		Tlog tlog = new Tlog();
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(zjfUser.getUserId() == null){
			
			tlog.setOpType(1);
			tlog.setContent("新增用户："+ zjfUser.getUserName());
			
			zjfUser.setUserId(RandomCode.makeCode());
			zjfUser.setCreateTime(DateTime.getDateTime());
			zjfUser.setCreateUser(tuser.getLoginName());
			if(this.saveNewZjfUser(zjfUser)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				
				if(zjfUser.getDisabledState()!=null && zjfUser.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("用户状态："+zjfUser.getUserName()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改用户："+zjfUser.getUserName());
				}
				
				zjfUser.setUpdateTime(DateTime.getDateTime());
				zjfUser.setUpdateUser(tuser.getLoginName());
				this.zjfUserDAO.update(zjfUser);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewZjfUser(ZjfUser zjfUser){
		try{
			this.zjfUserDAO.save(zjfUser);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	 
	/**
	 * 根据openId获取用户
	 * @param openID
	 * @return
	 */
	public ZjfUser getZjfUserByOpenId(String openID){
		
		List<ZjfUser> zjfUsers = this.zjfUserDAO.findByProperty("user_webchat_openID", openID);
		if(zjfUsers != null && zjfUsers.size() > 0){
			return zjfUsers.get(0);
		}
		
		return null;
		
	}
	
	/**
	 * 根据userId获取用户
	 * @param openID
	 * @return
	 */
	public ZjfUser getZjfUserById(String userId){
		
		return this.zjfUserDAO.findById(userId);
		
	}
	
	
	//注入需要的bean
	public void setZjfUserDAO(ZjfUserDAO zjfUserDAO) {
		this.zjfUserDAO = zjfUserDAO;
	}
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}

}
